<script>
import {deleteBatch, delMenu, edit, getIconList, save, selectAll} from "@/api/menu";

export default {
  name: "Menu",
  data() {
    return {
      tableData: [],
      name: "",
      loading: true,
      multipleSelection: [],
      form:{},
      dialogFormVisible:false,
      formLabelWidth: '100px',
      dialogFormVisible1:false,
      dialogFormVisible2:false,
      menuData:[],
      options:[]
    }
  },
  created() {
    this.load();
    this.getIcons();
  },
  methods: {
    load() {
       let params={
          name:this.name
        };
       selectAll(params).then(res => {
        this.tableData = res.data;
        this.loading = false;
      })
    },
    reset() {
      this.name = "";
      this.load();
    },
    handleDelete(id) {
      delMenu(id).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'error'
          });
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((a) => a.menuId);
      console.log(this.multipleSelection);
    },
    handleBatchRemove() {
      let ids = this.multipleSelection;
      deleteBatch(ids).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            center: true,
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    save(){
      save(this.form).then(res=>{
        if(res.code===200){
          this.$message({
            message: '菜单创建成功',
            type: 'success',
            center:true
          });
          this.dialogFormVisible=false;
          this.dialogFormVisible1=false;
          this.form={};
          this.load();
        }else{
          this.$message({
            message: res.msg,
            type: 'error',
            center:true
          });
        }
      })
    },
    edit(menuId){
      console.log(this.form);
      edit(this.form).then(res=>{
        if(res.code===200){
          this.$message({
            message: '修改成功',
            type: 'success',
            center:true
          });
          this.dialogFormVisible2=false;
          this.form={};
          this.load();
        }else{
          this.$message({
            message: res.msg,
            type: 'error',
            center:true
          });
        }
      })
    },
    handleAddSubmenu(menuId){
       if(menuId){
         this.dialogFormVisible1=true;
         this.form={};
         this.form.parentId=menuId;
       }
    },
    handleAdd(){
      this.form={};
      this.dialogFormVisible=true;
    },
    getIcons(){
      getIconList().then(res=>{
            if(res.code===200){
              this.options=res.data;
            }else{
              this.$message({
                message:  res.msg,
                type: 'error',
                center:true
              })
            }
      })
    },
    handleEdit(row){
      this.dialogFormVisible2=true;
      this.form=row;
    },
    headerBg({row,rowIndex}){
      if(rowIndex==0){
        return 'headerBgClass';
      }
    },
    changeEnable(val) {
      // this.request.post("/file/update", val).then(res => {
      //   if (res.code == 200) {
      //     this.$message({
      //       message: '更新成功',
      //       type: 'success',
      //       center: true
      //     });
      //     this.load();
      //   } else {
      //     this.$message({
      //       center: true,
      //       message: '更新失败',
      //       type: 'error'
      //     });
      //   }
      // })
    }
  }
}
</script>

<template>
  <div>
    <div>
      <el-input style="width:160px" v-model="name" suffix-icon="el-icon-search" placeholder="请输入菜单名称"></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
      <el-button type="warning" style="margin-left: 5px" @click="reset">重置</el-button>
    </div>
    <div style="margin:10px 0px">
      <el-button type="primary" class="ml-5" icon="el-icon-circle-plus-outline" @click="handleAdd">新增</el-button>
      <el-dialog  title="新增菜单" :visible.sync="dialogFormVisible" width="25%" center >
        <el-form :model="form" >
          <el-form-item label="菜单名称" :label-width="formLabelWidth">
            <el-input v-model="form.menuName" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="菜单路径" :label-width="formLabelWidth">
            <el-input v-model="form.path" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="页面路径" :label-width="formLabelWidth">
            <el-input v-model="form.pagePath" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="菜单图标" :label-width="formLabelWidth">
          <el-select v-model="form.icon" placeholder="请选择图标" style="width:100%">
            <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value">
            <i :class="item.value"></i>&emsp;<scan>{{item.name}}</scan>
            </el-option>
          </el-select>
          </el-form-item >
          <el-form-item label="显示排序" :label-width="formLabelWidth" >
            <el-input-number v-model="form.sortNum" controls-position="right" :min="1" :max="99"></el-input-number>
          </el-form-item>
          <el-form-item label="菜单描述" :label-width="formLabelWidth">
            <el-input v-model="form.description" autocomplete="off" clearable></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="save">确 定</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </div>
      </el-dialog>
      <el-popconfirm
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="你确定批量删除吗？"
          @confirm="handleBatchRemove"
          class="ml-5"
      >
        <el-button type="danger" class="ml-5" icon="el-icon-remove-outline" slot="reference">批量删除</el-button>
      </el-popconfirm>
    </div>
    <el-table :data="tableData"
              border stripe
              max-height="550px"
              row-key="menuId"
              v-loading="loading"
              :header-cell-class-name="headerBg"
              :tree-props="{children: 'submenuList',hasChildren:'hasChildren'}"
              element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255,225,255, 0.1)"
              @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"  align="center"></el-table-column>
      <el-table-column prop="menuId" label="ID" width="100px" sortable  align="center">
      </el-table-column>
      <el-table-column prop="menuName" label="菜单名称"  align="center">
      </el-table-column>
      <el-table-column prop="icon" label="菜单图标" align="center" class-name="fontSize18"  label-class-name="fontSize12">
        <template slot-scope="scope">
          <span :class="scope.row.icon"/>
        </template>
      </el-table-column>
      <el-table-column prop="sortNum" label="排序"  align="center">
      </el-table-column>
      <el-table-column prop="path" label="菜单路径" :show-overflow-tooltip=true  align="center">
      </el-table-column>
      <el-table-column prop="pagePath" label="页面路径" :show-overflow-tooltip=true  align="center">
      </el-table-column>
      <el-table-column prop="description" label="菜单描述" :show-overflow-tooltip=true  align="center">
      </el-table-column>
      <el-table-column label="操作" width="380px" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-plus" class="mr-5" @click="handleAddSubmenu(scope.row.menuId)" v-if="!scope.row.pagePath && !scope.row.path">新增子菜单</el-button>
          <el-dialog  title="新增子菜单" :visible.sync="dialogFormVisible1" width="25%" :modal-append-to-body="false"  center>
            <el-form :model="form" >
              <el-form-item label="菜单名称" :label-width="formLabelWidth">
                <el-input v-model="form.menuName" autocomplete="off" clearable></el-input>
              </el-form-item>
              <el-form-item label="菜单路径" :label-width="formLabelWidth" >
                <el-input v-model="form.path" autocomplete="off" clearable></el-input>
              </el-form-item>
              <el-form-item label="页面路径" :label-width="formLabelWidth">
                <el-input v-model="form.pagePath" autocomplete="off" clearable></el-input>
              </el-form-item>
              <el-form-item label="菜单图标" :label-width="formLabelWidth">
                <el-select v-model="form.icon" placeholder="请选择图标" style="width:100%">
                  <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value">
                    <i :class="item.value"></i>&emsp;<scan>{{item.name}}</scan>
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="显示排序" :label-width="formLabelWidth" >
                <el-input-number v-model="form.sortNum" controls-position="right" :min="1" :max="99"></el-input-number>
              </el-form-item>
              <el-form-item label="菜单描述" :label-width="formLabelWidth">
                <el-input v-model="form.description" autocomplete="off" clearable></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="save">确 定</el-button>
              <el-button @click="dialogFormVisible1 = false">取 消</el-button>
            </div>
          </el-dialog>
          <el-button type="success" icon="el-icon-edit" style="margin-left: 5px" @click="handleEdit(scope.row)">编辑</el-button>
          <el-dialog  title="编辑菜单" :visible.sync="dialogFormVisible2" width="25%" :modal-append-to-body=false center>
            <el-form :model="form" >
              <el-form-item label="菜单名称" :label-width="formLabelWidth">
                <el-input v-model="form.menuName" autocomplete="off" clearable></el-input>
              </el-form-item>
              <el-form-item label="菜单路径" :label-width="formLabelWidth">
                <el-input v-model="form.path" autocomplete="off" clearable></el-input>
              </el-form-item>
              <el-form-item label="页面路径" :label-width="formLabelWidth">
                <el-input v-model="form.pagePath" autocomplete="off" clearable></el-input>
              </el-form-item>
              <el-form-item label="菜单图标" :label-width="formLabelWidth">
                <el-select v-model="form.icon" placeholder="请选择图标" style="width:100%">
                  <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value">
                    <i :class="item.value"></i>&emsp;<scan>{{item.name}}</scan>
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="显示排序" :label-width="formLabelWidth" >
                <template>
                <el-input-number v-model="form.sortNum" controls-position="right" :min="1" :max="99" />
                </template>
              </el-form-item>
              <el-form-item label="菜单描述" :label-width="formLabelWidth">
                <el-input v-model="form.description" autocomplete="off" clearable></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="edit(scope.row.menuId)">确 定</el-button>
              <el-button @click="dialogFormVisible2 = false">取 消</el-button>
            </div>
          </el-dialog>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="handleDelete(scope.row.menuId)"
          >
            <el-button type="danger" icon="el-icon-delete" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style scoped>
.fontSize18{
  font-size:18px
}
.fontSize12{
  font-size:12px
}
.headerBgClass{
  background-color: #cccccc !important;
}
</style>
